home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / PROPSHET.PAK / PROPSHET.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  17KB  |  621 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   propshet.c
  9. //
  10. //  PURPOSE:   Implement a basic property sheet dialog.
  11. //
  12. //  FUNCTIONS:
  13. //    WndProc      - Processes messages for the main window.
  14. //    MsgCreate    - Handle the WM_CREATE message for the main window.
  15. //    MsgCommand   - Handle the WM_COMMAND messages for the main window.
  16. //    MsgPaint     - Handle the WM_PAINT message for the main window.
  17. //    MsgDestroy   - Handles the WM_DESTROY message by calling 
  18. //                   PostQuitMessage().
  19. //    CmdExit      - Handles the file exit command by calling destory 
  20. //                   window on the main window.
  21. //
  22. //  COMMENTS:
  23. //
  24.  
  25. #include <windows.h>            // required for all Windows applications
  26. #include <windowsx.h>
  27. #include "globals.h"            // prototypes specific to this application
  28. #include "resource.h"
  29.  
  30.  
  31. // Main window message table definition.
  32. MSD rgmsd[] =
  33. {
  34.     {WM_COMMAND, MsgCommand},
  35.     {WM_PAINT,   MsgPaint},
  36.     {WM_CREATE,  MsgCreate},
  37.     {WM_DESTROY, MsgDestroy}
  38. };
  39.  
  40. MSDI msdiMain =
  41. {
  42.     sizeof(rgmsd) / sizeof(MSD),
  43.     rgmsd,
  44.     edwpWindow
  45. };
  46.  
  47.  
  48. // Main window command table definition.
  49. CMD rgcmd[] =
  50. {
  51.     {IDM_PROPSHET, CmdPropShet},
  52.     {IDM_EXIT,  CmdExit},
  53.     {IDM_ABOUT, CmdAbout}
  54. };
  55.  
  56. CMDI cmdiMain =
  57. {
  58.     sizeof(rgcmd) / sizeof(CMD),
  59.     rgcmd,
  60.     edwpWindow
  61. };
  62.  
  63. HBRUSH ghBrushRed;  
  64. HBRUSH ghBrushGreen;
  65. HBRUSH ghBrushBlue; 
  66. WPARAM   ghDlgBrushCtlID;
  67.  
  68. LRESULT CALLBACK BackgrndDialogProc(HWND, UINT, WPARAM, LPARAM);
  69. LRESULT CALLBACK ShapeDialogProc(HWND, UINT, WPARAM, LPARAM);
  70. int DoPropertySheet(HWND);
  71.  
  72.  
  73. //
  74. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  75. //
  76. //  PURPOSE:  Processes messages for the main window.
  77. //
  78. //  PARAMETERS:
  79. //    hwnd     - window handle
  80. //    uMessage - message number
  81. //    wparam   - additional information (dependant on message number)
  82. //    lparam   - additional information (dependant on message number)
  83. //
  84. //  RETURN VALUE:
  85. //    The return value depends on the message number.  If the message
  86. //    is implemented in the message dispatch table, the return value is
  87. //    the value returned by the message handling function.  Otherwise,
  88. //    the return value is the value returned by the default window procedure.
  89. //
  90. //  COMMENTS:
  91. //    Call the DispMessage() function with the main window's message dispatch
  92. //    information (msdiMain) and the message specific information.
  93. //
  94.  
  95. LRESULT CALLBACK WndProc(HWND   hwnd, 
  96.                          UINT   uMessage, 
  97.                          WPARAM wparam, 
  98.                          LPARAM lparam)
  99. {
  100.     return DispMessage(&msdiMain, hwnd, uMessage, wparam, lparam);
  101. }
  102.  
  103.  
  104. //
  105. //  FUNCTION: MsgCreate(HWND, UINT, WPARAM, LPARAM)
  106. //
  107. //  PURPOSE: Handle the WM_CREATE messages for the main window.
  108. //
  109. //  PARAMETERS:
  110. //    hwnd     - window handle
  111. //    uMessage - WM_CREATE
  112. //    wparam   - Not used
  113. //    lparam)  - Not used
  114. //
  115. //  RETURN VALUE:
  116. //    Always return 0
  117. //
  118. //  COMMENTS:
  119. //    This function will create 2 brushes to be used for painting the
  120. //    client area.
  121. //
  122.  
  123. #pragma argsused
  124. LRESULT MsgCreate(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  125. {
  126.     ghBrushRed   = CreateSolidBrush(RGB(255, 0, 0));
  127.     ghBrushGreen = CreateSolidBrush(RGB(0, 255, 0));
  128.     ghBrushBlue  = CreateSolidBrush(RGB(0, 0, 255));
  129.  
  130.      return 0;
  131. }
  132.  
  133.  
  134. //
  135. //  FUNCTION: MsgCommand(HWND, UINT, WPARAM, LPARAM)
  136. //
  137. //  PURPOSE: Handle the WM_COMMAND messages for the main window.
  138. //
  139. //  PARAMETERS:
  140. //    hwnd     - window handle
  141. //    uMessage - WM_COMMAND (Unused)
  142. //    GET_WM_COMMAND_ID(wparam, lparam)   - Command identifier
  143. //    GET_WM_COMMAND_HWND(wparam, lparam) - Control handle
  144. //
  145. //  RETURN VALUE:
  146. //    The return value depends on the message number.  If the message
  147. //    is implemented in the message dispatch table, the return value is
  148. //    the value returned by the message handling function.  Otherwise,
  149. //    the return value is the value returned by the default window procedure.
  150. //
  151. //  COMMENTS:
  152. //    Call the DispCommand() function with the main window's command dispatch
  153. //    information (cmdiMain) and the command specific information.
  154. //
  155.  
  156. #pragma argsused
  157. LRESULT MsgCommand(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  158. {
  159.      return DispCommand(&cmdiMain, hwnd, wparam, lparam);
  160. }
  161.  
  162.  
  163. //
  164. //  FUNCTION: MsgPaint(HWND, UINT, WPARAM, LPARAM)
  165. //
  166. //  PURPOSE: Paints the client area.
  167. //
  168. //  PARAMETERS:
  169. //
  170. //    hwnd      - Window handle  (Unused)
  171. //    uMessage  - Message number (Unused)
  172. //    wparam    - Extra data     (Unused)
  173. //    lparam    - Extra data     (Unused)
  174. //
  175. //  RETURN VALUE:
  176. //
  177. //    Always returns 0 - Message handled
  178. //
  179. //  COMMENTS:
  180. //
  181. //
  182.  
  183. LRESULT MsgPaint(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  184. {
  185.     PAINTSTRUCT ps;
  186.     HDC         hDC;
  187.     RECT        rect;
  188.     HBRUSH      hBrush;
  189.     WORD        wShape, x, y, wWidth = 0, wHeight = 0;
  190.  
  191.     // Because we are checking the return value of GetProp(), the function
  192.     // failure reported by CodeGuard on the line below can safely be ignored.
  193.     // GetProp() is used here and elsewhere in this program to determine if
  194.     // a property has been set, and thus failures are expected and constitute
  195.     // correct behavior.
  196.  
  197.     if (GetProp(hwnd, "Brush") || GetProp(hwnd, "Shape"))
  198.     {
  199.         hDC = BeginPaint(hwnd, &ps);
  200.           GetClientRect(hwnd, &rect);
  201.  
  202.           hBrush = (HBRUSH)GetProp(hwnd, "Brush");
  203.           if (hBrush)
  204.                 FillRect(hDC, &rect, hBrush);
  205.  
  206.           wShape = (WORD)GetProp(hwnd, "Shape");
  207.           if (wShape)
  208.           {
  209.             switch(wShape)
  210.             {
  211.                 case IDC_RECTSQUARE:   
  212.                     wWidth  = 15;
  213.                     wHeight = 15;
  214.                     break;
  215.  
  216.                 case IDC_TRAPEZOID:
  217.                     wWidth  = 15;
  218.                     wHeight = 15;
  219.                     break;
  220.  
  221.                 case IDC_RECTANGLE:
  222.                     wWidth  = 25;
  223.                     wHeight = 15;
  224.                     break;
  225.  
  226.                 default:
  227.                     break;
  228.             }
  229.  
  230.             //First draw the vertical lines...
  231.  
  232.             for (x = wWidth; x < rect.right; x += wWidth)
  233.             {
  234.                 MoveToEx(hDC, x, 0, NULL);
  235.                 LineTo(hDC, x, rect.bottom);
  236.                 if (wShape == IDC_TRAPEZOID)
  237.                     for (y = 0; y < rect.bottom; y += wHeight)
  238.                     {
  239.                         MoveToEx(hDC, x, y, NULL);
  240.                         LineTo(hDC, x-wWidth, y+wHeight);
  241.                     }
  242.             }
  243.  
  244.             //Then draw the horizontal lines
  245.  
  246.             if (wShape != IDC_TRAPEZOID)
  247.             {
  248.                 for (y = wHeight; y < rect.bottom; y += wHeight)
  249.                 {
  250.                     MoveToEx(hDC, 0, y, NULL);
  251.                     LineTo(hDC, rect.right, y);
  252.                 }
  253.             }
  254.         }
  255.  
  256.         EndPaint(hwnd, &ps);
  257.         return 0;
  258.     }
  259.     else
  260.         return DefWindowProc(hwnd, uMessage, wparam, lparam);
  261. }
  262.  
  263.  
  264. //
  265. //  FUNCTION: MsgDestroy(HWND, UINT, WPARAM, LPARAM)
  266. //
  267. //  PURPOSE: Calls PostQuitMessage().
  268. //
  269. //  PARAMETERS:
  270. //
  271. //    hwnd      - Window handle  (Unused)
  272. //    uMessage  - Message number (Unused)
  273. //    wparam    - Extra data     (Unused)
  274. //    lparam    - Extra data     (Unused)
  275. //
  276. //  RETURN VALUE:
  277. //
  278. //    Always returns 0 - Message handled
  279. //
  280. //  COMMENTS:
  281. //
  282. //
  283.  
  284. #pragma argsused
  285. LRESULT MsgDestroy(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  286. {
  287.      if (GetProp(hwnd, "Brush"))
  288.           RemoveProp(hwnd, "Brush");
  289.  
  290.      if (GetProp(hwnd, "Shape"))
  291.           RemoveProp(hwnd, "Shape");
  292.  
  293.      if (ghBrushRed)   DeleteObject(ghBrushRed);
  294.      if (ghBrushGreen) DeleteObject(ghBrushGreen);
  295.      if (ghBrushBlue)  DeleteObject(ghBrushBlue);
  296.  
  297.      PostQuitMessage(0);
  298.  
  299.     return 0;
  300. }
  301.  
  302.  
  303. //
  304. //  FUNCTION: CmdPropShet(HWND, WORD, WORD, HWND)
  305. //
  306. //  PURPOSE: Bring up a property sheet dialog
  307. //
  308. //  PARAMETERS:
  309. //    hwnd     - The owner window for the property sheet (main window in this case).
  310. //    wCommand - IDM_PROPSHET (unused)
  311. //    wNotify  - Notification number (unused)
  312. //    hwndCtrl - NULL (unused)
  313. //
  314. //  RETURN VALUE:
  315. //    Always returns 0 - command handled.
  316. //
  317. //  COMMENTS:
  318. //
  319. //
  320.  
  321. #pragma argsused
  322. LRESULT CmdPropShet(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  323. {
  324.      if (-1 == DoPropertySheet(hwnd))
  325.           MessageBox(hwnd,
  326.                          "Failure creating Property Sheet!",
  327.                          szAppName,
  328.                    MB_OK);
  329.     return 0;
  330. }
  331.  
  332. //
  333. //  FUNCTION: CmdExit(HWND, WORD, WORD, HWND)
  334. //
  335. //  PURPOSE: Exit the application.
  336. //
  337. //  PARAMETERS:
  338. //    hwnd     - The window.
  339. //    wCommand - IDM_EXIT (unused)
  340. //    wNotify  - Notification number (unused)
  341. //    hwndCtrl - NULL (unused)
  342. //
  343. //  RETURN VALUE:
  344. //    Always returns 0 - command handled.
  345. //
  346. //  COMMENTS:
  347. //
  348. //
  349.  
  350. #pragma argsused
  351. LRESULT CmdExit(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  352. {
  353.     DestroyWindow(hwnd);
  354.     return 0;
  355. }
  356.  
  357.  
  358. //
  359. //  FUNCTION: DoPropertySheet(HWND)
  360. //
  361. //  PURPOSE: Fills out the property sheet data structures and displays
  362. //           the dialog with the property sheets.
  363. //
  364. //  PARAMETERS:
  365. //
  366. //    hwndOwner  - Parent window handle of the property sheets
  367. //
  368. //  RETURN VALUE:
  369. //
  370. //    Returns value from PropertySheet()
  371. //
  372. //  COMMENTS:
  373. //
  374. //
  375.  
  376. int DoPropertySheet(HWND hwndOwner)
  377. {
  378.     PROPSHEETPAGE psp[2];
  379.     PROPSHEETHEADER psh;
  380.  
  381.     //Fill out the PROPSHEETPAGE data structure for the Background Color
  382.     //sheet
  383.  
  384.     psp[0].dwSize = sizeof(PROPSHEETPAGE);
  385.     psp[0].dwFlags = PSP_USEICONID | PSP_USETITLE;
  386.      psp[0].hInstance = hInst;
  387.  
  388.      // PROPSHEETPAGE uses anonymous unions, which are not supported in C.
  389.      // The code below is conditionalized to show you how the usage differs
  390.      // between C and C++.
  391.      #if defined (__cplusplus)
  392.      psp[0].pszTemplate = MAKEINTRESOURCE(dlgbackcolor);
  393.      psp[0].pszIcon = MAKEINTRESOURCE(IDI_BACKCOLOR);
  394.      #else
  395.      psp[0].DUMMYUNIONNAME.pszTemplate = MAKEINTRESOURCE(dlgbackcolor);
  396.      psp[0].DUMMYUNIONNAME2.pszIcon = MAKEINTRESOURCE(IDI_BACKCOLOR);
  397.      #endif
  398.  
  399.      psp[0].pfnDlgProc = (DLGPROC) BackgrndDialogProc;
  400.      psp[0].pszTitle = "Background Color";
  401.      psp[0].lParam = 0;
  402.  
  403.     //Fill out the PROPSHEETPAGE data structure for the Client Area Shape
  404.     //sheet
  405.  
  406.     psp[1].dwSize = sizeof(PROPSHEETPAGE);
  407.     psp[1].dwFlags = PSP_USEICONID | PSP_USETITLE;
  408.      psp[1].hInstance = hInst;
  409.  
  410.      // PROPSHEETPAGE uses anonymous unions, which are not supported in C.
  411.      // The code below is conditionalized to show you how the usage differs
  412.      // between C and C++.
  413.      #if defined (__cplusplus)
  414.      psp[1].pszTemplate = MAKEINTRESOURCE(dlgshape);
  415.      psp[1].pszIcon = MAKEINTRESOURCE(IDI_SHAPES);
  416.      #else
  417.      psp[1].DUMMYUNIONNAME.pszTemplate = MAKEINTRESOURCE(dlgshape);
  418.      psp[1].DUMMYUNIONNAME2.pszIcon = MAKEINTRESOURCE(IDI_SHAPES);
  419.      #endif
  420.  
  421.      psp[1].pfnDlgProc = (DLGPROC) ShapeDialogProc;
  422.      psp[1].pszTitle = "Client Area Shape";
  423.      psp[1].lParam = 0;
  424.  
  425.      //Fill out the PROPSHEETHEADER
  426.  
  427.     psh.dwSize = sizeof(PROPSHEETHEADER);
  428.     psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE;
  429.      psh.hwndParent = hwndOwner;
  430.      psh.hInstance = hInst;
  431.  
  432.      // PROPSHEETPAGE uses anonymous unions, which are not supported in C.
  433.      // The code below is conditionalized to show you how the usage differs
  434.      // between C and C++.
  435.      #if defined (__cplusplus)
  436.      psh.pszIcon = "";
  437.      psh.ppsp = (LPCPROPSHEETPAGE) &psp;
  438.      #else
  439.      psh.DUMMYUNIONNAME.pszIcon = "";
  440.      psh.DUMMYUNIONNAME3.ppsp = (LPCPROPSHEETPAGE) &psp;
  441.      #endif
  442.  
  443.      psh.pszCaption = (LPSTR)"Color Properties";
  444.      psh.nPages = sizeof(psp) / sizeof(PROPSHEETPAGE);
  445.  
  446.     //And finally display the dialog with the two property sheets.
  447.  
  448.     return PropertySheet(&psh);
  449. }
  450.  
  451.  
  452. //
  453. //  FUNCTION: BackgrndDialogProc(HWND, UINT, WPARAM, LPARAM)
  454. //
  455. //  PURPOSE:  Processes messages for background color property sheet.
  456. //
  457. //  PARAMETERS:
  458. //    hdlg - window handle of the property sheet
  459. //    wMessage - type of message
  460. //    wparam - message-specific information
  461. //    lparam - message-specific information
  462. //
  463. //  RETURN VALUE:
  464. //    TRUE - message handled
  465. //    FALSE - message not handled
  466. //
  467. //  COMMENTS:  In the interest of simplicity, we will stick to the
  468. //             old style dialog procedure.  The point of this sample
  469. //             is to demonstrate how to implement the PropertySheet
  470. //             control.
  471. //
  472.  
  473. LRESULT CALLBACK BackgrndDialogProc(HWND hdlg, 
  474.                                     UINT uMessage, 
  475.                                     WPARAM wparam, 
  476.                                     LPARAM lparam)
  477. {
  478.     HWND hWndApp;
  479.     LPNMHDR lpnmhdr;
  480.  
  481.     switch (uMessage)
  482.     {
  483.         case WM_NOTIFY:
  484.             lpnmhdr = (NMHDR FAR *)lparam;
  485.  
  486.             switch (lpnmhdr->code)
  487.             {
  488.                 case PSN_SETACTIVE:
  489.                     if (ghDlgBrushCtlID)
  490.                         SendMessage(GetDlgItem(hdlg, ghDlgBrushCtlID),
  491.                                     BM_SETCHECK,
  492.                                     TRUE,
  493.                                     0L);
  494.                     else
  495.                         SendMessage(GetDlgItem(hdlg, IDC_RED),
  496.                                     BM_SETCHECK,
  497.                                     TRUE,
  498.                                     0L);
  499.                     break;
  500.  
  501.                  default:
  502.                     break;
  503.             }
  504.  
  505.             break;
  506.  
  507.         case WM_COMMAND:
  508.             hWndApp = GetParent(GetParent(hdlg));
  509.  
  510.             switch (wparam)
  511.                 {
  512.                 case IDC_RED:
  513.                     ghDlgBrushCtlID = wparam;
  514.                     SetProp(hWndApp, "Brush", (HBRUSH)ghBrushRed);
  515.                     break;
  516.  
  517.                 case IDC_GREEN:
  518.                     ghDlgBrushCtlID = wparam;
  519.                     SetProp(hWndApp, "Brush", (HBRUSH)ghBrushGreen);
  520.                     break;
  521.  
  522.                 case IDC_BLUE:
  523.                     ghDlgBrushCtlID = wparam;
  524.                     SetProp(hWndApp, "Brush", (HBRUSH)ghBrushBlue);
  525.                     break;
  526.  
  527.                 default:
  528.                     break;
  529.             }
  530.  
  531.             InvalidateRect(hWndApp, NULL, TRUE);
  532.             UpdateWindow(hWndApp);
  533.             break;
  534.  
  535.         default:
  536.             break;
  537.     }
  538.  
  539.     return FALSE;
  540. }
  541.  
  542.  
  543. //
  544. //  FUNCTION: ShapeDialogProc(HWND, UINT, WPARAM, LPARAM)
  545. //
  546. //  PURPOSE:  Processes messages for "Rectangle" property sheet.
  547. //
  548. //  PARAMETERS:
  549. //    hdlg - window handle of the property sheet
  550. //    wMessage - type of message
  551. //    wparam - message-specific information
  552. //    lparam - message-specific information
  553. //
  554. //  RETURN VALUE:
  555. //    TRUE - message handled
  556. //    FALSE - message not handled
  557. //
  558. //  COMMENTS:  In the interest of simplicity, we will stick to the
  559. //             old style dialog procedure.  The point of this sample
  560. //             is to demonstrate how to implement the PropertySheet
  561. //             control.
  562. //
  563.  
  564. LRESULT CALLBACK ShapeDialogProc(HWND hdlg, 
  565.                                   UINT uMessage, 
  566.                                   WPARAM wparam, 
  567.                                  LPARAM lparam)
  568. {
  569.     HWND hWndApp;
  570.     WORD wID;
  571.     LPNMHDR lpnmhdr;
  572.  
  573.     switch (uMessage)
  574.     {
  575.         case WM_NOTIFY:
  576.             lpnmhdr = (NMHDR FAR *)lparam;
  577.             hWndApp = GetParent(GetParent(hdlg));
  578.  
  579.             switch (lpnmhdr->code)
  580.                 {
  581.                 case PSN_SETACTIVE:
  582.                           wID = (WORD)GetProp(hWndApp, "Shape");
  583.                           if (wID)
  584.                         SendMessage(GetDlgItem(hdlg, wID),
  585.                                     BM_SETCHECK,
  586.                                     TRUE,
  587.                                     0L);
  588.                     break;
  589.  
  590.                  default:
  591.                     break;
  592.             }
  593.  
  594.             break;
  595.  
  596.         case WM_COMMAND:
  597.             hWndApp = GetParent(GetParent(hdlg));
  598.  
  599.             switch (wparam)
  600.             {
  601.                 case IDC_RECTSQUARE:
  602.                 case IDC_TRAPEZOID:
  603.                 case IDC_RECTANGLE:
  604.                     SetProp(hWndApp, "Shape", (HANDLE)(WORD)wparam);
  605.                     break;
  606.  
  607.                 default:
  608.                     break;
  609.             }
  610.  
  611.             InvalidateRect(hWndApp, NULL, TRUE);
  612.             UpdateWindow(hWndApp);
  613.             break;
  614.  
  615.         default:
  616.             break;
  617.     }
  618.  
  619.     return FALSE;
  620. }
  621.